$(function(){
  //console.log(1);
  const id = window.location.search.split("=")[1];
  //console.log(id);
  $.ajax({
    method: 'get',
    url: 'http://localhost:8080/api/v1/index/article',
    data: {id},
    success: (res) => {
      console.log(res);
      if(res.code===200){
        $('.article_title').text(res.data.title);
        $('.article_con').html(res.data.content);
        const htmlStr = template('author_list',res);
        $('.article_info').html(htmlStr);
      }
    }
  })
  // 获取评论数据
  serach()
  function serach(){
    $.ajax({
      url: 'http://localhost:8080/api/v1/index/get_comment',
      type: 'get',
      dataType: 'json',
      data:{
          articleId:id
      },
      success: function (res) {
          console.log(res);
          $('.comment_list_con').html(template('comment', {list:res.data}));
          //根据数组长度显示评论条数
          $('.comment_count').text(res.data.length + '条评论');
      }
  });
  }
//3.发表评论按钮点击事件
//表单按钮：禁用默认事件
$('.comment_sub').click(function (e) {
  //1.禁用默认行为
  e.preventDefault();
  const name=$('.comment_name').val().trim();
  const input=$('.comment_input').val().trim()
  //2.非空判断
  if(name===''||input===''){
    return alert('请输入数据')
  }
  //3.ajax请求
  $.ajax({
      url: 'http://localhost:8080/api/v1/index/post_comment',
      type: 'post',
      dataType: 'json',
      data: {
          author: name,
          content: name,
          articleId: id
      },
      success: function (res) {
          console.log(res);
          if (res.code == 201) {
              alert('发表成功');
              window.location.reload();
              serach()
          }
      }
  });
});
})